<template>
  <view class="uni-tabs__header">
    <view class="uni-tabs__nav-wrap">
      <scroll-view scroll-x class="uni-tabs__nav-scroll">
        <view class="uni-tabs__nav">
          <view v-for="(item,key) in list" :key="key" @click="switchTab(item.value)"
            :class="{'is-active':current===item.value}" class="uni-tabs__item">
            {{item.name}}
            <uni-badge v-if="item.error" class="menu-badge" :text="item.error" type="error"></uni-badge>
          </view>

          <view v-if="actionTab" @click="actionTabItem" class="uni-tabs__item uni-tabs__add">
            <view style="display: flex;align-items: center;">
              <text style="margin-left: 3px;">{{actionText}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "uni-tabs",
    data() {
      return {};
    },
    props: {
      list: {
        type: Array,
        default () {
          return []
        },
      },
      current: {
        type: String
      },
      actionTab: {
        type: Boolean,
        default: false
      },
      actionText: {
        type: String,
        default: ""
      },
    },
    methods: {
      switchTab(tab) {
        if (this.current == tab) {
          return;
        }        
        this.$emit("update:current", tab);
        this.$emit("clickTab", tab);
      },
      actionTabItem() {
        this.$emit("action")
      }
    },
  }
</script>

<style lang="scss" scoped>
  .uni-tabs__header {
    position: relative;
    background-color: #F8F8F8;
    border-bottom: 1px solid #e4e7ed;

  }

  .uni-tabs__nav-wrap {
    overflow: hidden;
    margin-bottom: -1px;
    position: relative;
  }

  .uni-tabs__nav-scroll {
    width: 100%;
    overflow: hidden;
  }

  .uni-tabs__nav {
    position: relative;
    white-space: nowrap;
  }

  .uni-tabs__item {
    position: relative;
    padding: 0 20px;
    height: 40px;
    box-sizing: border-box;
    line-height: 40px;
    display: inline-block;
    list-style: none;
    font-size: 14px;
    font-weight: 500;
    color: #909399;
    margin-top: -1px;
    margin-left: -1px;
    border: 1px solid transparent;
    cursor: pointer;
  }

  .uni-tabs__item.is-active {
    color: $uni-color-primary;
    background-color: #fff;
    border-right-color: #dcdfe6;
    border-left-color: #dcdfe6;
  }

  .uni-tabs__add:hover {
    transition: 0.2s;
    background-color: white;
    color: $uni-color-primary;

  }
</style>